package com.musicdaze;

import java.util.ArrayList;
import java.util.List;

import com.musicdaze.itunes.ClassFactory;
import com.musicdaze.itunes.IITPlaylist;
import com.musicdaze.itunes.IITPlaylistCollection;
import com.musicdaze.itunes.IITTrack;
import com.musicdaze.itunes.IITTrackCollection;
import com.musicdaze.itunes.IiTunes;


/**
 * Main connector to all things iTunes
 * @author mfujihara
 *
 */
public class ItunesController {
	/** the COM connection to Itunes */
	private final static IiTunes itunes;
	/** the only instance */
	private static ItunesController instance;
	static{
		itunes = ClassFactory.createiTunesApp();
	}
	/** the loaded tracks */
	public List<TrackWrapper> tracks;
	/** the loaded playlists */
	public List<PlaylistWrapper> playlists;
	/**
	 * Constructs an ItunesController by getting the COM
	 * connection and loading the tracks from iTunes
	 */
	private ItunesController(){
		tracks = new ArrayList<TrackWrapper>();
		playlists = new ArrayList<PlaylistWrapper>();
		loadLibrary();
		loadPlaylists();
	}
	/**
	 * Loads the playlists
	 */
	private void loadPlaylists() {
		System.out.println("[Controller] Loading Playlists ...");
		IITPlaylistCollection lists = itunes.librarySource().playlists();
		playlists.clear();
		for (int i=1;i<=lists.count();i++){
			playlists.add(new PlaylistWrapper(lists.item(i)));
		}
		System.out.println("[Controller] Playlists Loaded.");
	}
	/**
	 * loads the tracks from the iTunes library
	 */
	private void loadLibrary(){
		System.out.println("[Controller] Loading Tsracks ...");
		tracks.clear();
		int count = itunes.libraryPlaylist().tracks().count();
		IITTrackCollection coll = itunes.libraryPlaylist().tracks();
		for (int i=1;i<=count;i++){
			tracks.add(new TrackWrapper(coll.item(i)));
		}
		System.out.println("[Controller] Tracks Loaded");
	}
	/**
	 * Gets the itunes connection
	 * @return the Itunes connection
	 */
	public IiTunes getItunes() {
		return itunes;
	}
	/**
	 * Gets the instance
	 * @return the instance
	 */
	public static ItunesController getInstance() {
		if (instance==null){
			instance = new ItunesController();
			Runtime.getRuntime().addShutdownHook(new Thread(new Runnable(){
				@Override
				public void run() {
					itunes.dispose();
				}}));
		}
		return instance;
	}
	/**
	 * A wrapper for the iTunes tracks
	 * @author mfujihara
	 *
	 */
	public static class TrackWrapper implements Playable{
		/** the wrapped track */
		public IITTrack track;

		/**
		 * Constructs a TrackWrapper to wrap the given Track
		 * @param track
		 */
		public TrackWrapper(IITTrack track){
			this.track = track;
		}
		/**
		 * Prints the wrapped track as a string
		 */
		public String toString(){
			return track.name();
		}
		/** {@inheritDoc} */
		@Override
		public void play() {
			track.play();
		}
	}	/**
	 * A wrapper for the iTunes tracks
	 * @author mfujihara
	 *
	 */
	public static class PlaylistWrapper implements Playable{
		/** the wrapped track */
		public IITPlaylist playlist;

		/**
		 * Constructs a TrackWrapper to wrap the given Track
		 * @param track
		 */
		public PlaylistWrapper(IITPlaylist playlist){
			this.playlist = playlist;
		}
		/**
		 * Prints the wrapped playlist as a string
		 */
		public String toString(){
			return playlist.name();
		}
		/** {@inheritDoc}*/
		@Override
		public void play() {
			playlist.playFirstTrack();
		}
	}

}
